<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    
    $this->view->title = "Mes Albums";
    $this->view->headTitle($this->view->title, 'PREPEND');
    $albums = new Model_DbTable_Albums();
    $this->view->albums = $albums->fetchAll();
    
    }
    
	function ajouterAction()
	{
	    $this->view->title = "Ajouter un nouvel album";
	    $this->view->headTitle($this->view->title, 'PREPEND');
	
	    $form = new Form_Album();
	    $form->envoyer->setLabel('Ajouter');
	    $this->view->form = $form;
	
	    if ($this->getRequest()->isPost()) {
	        $formData = $this->getRequest()->getPost();
	        if ($form->isValid($formData)) {
	            $artiste = $form->getValue('artiste');
	            $titre = $form->getValue('titre');
	            $albums = new Model_DbTable_Albums();
	            $albums->ajouterAlbum($artiste, $titre);
	
	            $this->_redirect('/');
	        } else {
	            $form->populate($formData);
	        }
	    }
	}

	function modifierAction()
	{
    $this->view->title = "Modifier l'album";
    $this->view->headTitle($this->view->title, 'PREPEND');

    $form = new Form_Album();
    $form->envoyer->setLabel('Sauvegarder');
    $this->view->form = $form;

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $id = (int)$form->getValue('id');
            $artiste = $form->getValue('artiste');
            $titre = $form->getValue('titre');
            $albums = new Model_DbTable_Albums();
            $albums->modifierAlbum($id, $artiste, $titre);

            $this->_redirect('/');
        } else {
            $form->populate($formData);
        }
    } else {
        $id = $this->_getParam('id', 0);
        if ($id > 0) {
            $albums = new Model_DbTable_Albums();
            $form->populate($albums->obtenirAlbum($id));
        }
    }
}

	public function supprimerAction()
	{
	    $this->view->title = "Supprimer l'album";
	    $this->view->headTitle($this->view->title, 'PREPEND');
	
	    if ($this->getRequest()->isPost()) {
	        $supprimer = $this->getRequest()->getPost('supprimer');
	        if ($supprimer == 'Oui') {
	            $id = $this->getRequest()->getPost('id');
	            $albums = new Model_DbTable_Albums();
	            $albums->supprimerAlbum($id);
	        }
	
	        $this->_redirect('/');
	    } else {
	        $id = $this->_getParam('id', 0);
	        $albums = new Model_DbTable_Albums();
	        $this->view->album = $albums->obtenirAlbum($id);
	    }
	}
		

}

